#ifndef SQLPOOLRAII_H
#define SQLPOOLRAII_H
#include"sqlpool.h"
#include <assert.h>

// 使用RAII机制优化数据库连接的获取与释放

class SqlpoolRAII{
public:
    SqlpoolRAII(MYSQLPTR &conn, SqlPool *pool){
        assert(pool); // 如果传入的pool是一个空指针，将会终止整个程序的运行
        conn = pool->getConn();
        // 保存在类内一份
        _conn = conn;
        _pool = pool;
    }
    ~SqlpoolRAII(){
        if(_conn){
            _pool->freeConn(_conn);
        }
    }
private:
    MYSQLPTR _conn;
    SqlPool *_pool;
};

#endif